#include "opencv2/opencv.hpp"
#include <cstring>
#include "ncnn/net.h"
#include "ncnn/benchmark.h"
#include "retinaface.h"
#include <iostream>


#ifndef FACEVERIY_DETECTOR_H
#define FACEVERIY_DETECTOR_H
using namespace cv;
using namespace std;
using namespace retina;


namespace detect {
    struct FaceResult {
        int flag = 0;
        string logStr = "";
        cv::Rect faceRect = {};
        cv::Rect detRect = {};
        double iouNormalRatio = 0.0;
        double iouInterRatio = 0.0;
        retina::FaceObject object;
    };
    struct Status {
        int FaceStatus = 0; // 是否有人脸 包含人脸倾斜 多张人脸 人脸出境--->前端：对准屏幕
        int Luminance = 0; // 亮度--->前端：光线充足
        int Blurry = 0; // 人脸模糊--->前端：保持清晰
        string logStr = "";
        int costTime = 0.0;
        cv::Rect faceRect = {};
        cv::Rect detRect = {};
        int inputModelWidth = 0;
        int inputModelHeight = 0;
        retina::FaceObject object;
        vector<vector<float>> landmark;
//        int FaceSize = 0; // 人脸大小是否合理
//        int FaceOverCamera = 0; // 人脸出境
//        int inCline = 0; // 人脸倾斜
    };

    detect::Status predict(cv::Mat &image, float luminanceMiniThresh, float luminanceMaxThresh,
float blueThresh, float iouNormal, float iouInter, float TLXRatio, float TLYRatio, float RBXRatio, float RBYRatio);

    detect::Status predict(char *buffer, long file_size, float luminanceMiniThresh, float luminanceMaxThresh,
float blueThresh, float iouNormal, float iouInter, float TLXRatio, float TLYRatio, float RBXRatio, float RBYRatio);


    detect::Status main_func(cv::Mat &image, Status &image_status, float luminanceMiniThresh, float luminanceMaxThresh,
float blueThresh, float iouNormal, float iouInter, float TLXRatio, float TLYRatio, float RBXRatio, float RBYRatio);

    class Detector {
    public:
        static Detector *GetInstance() { // 返回类指针
            return g_pDetector;
        };

        __attribute__((unused)) static void deleteInstance() { // 删除指针
            if (g_pDetector) {
                delete g_pDetector;
                g_pDetector = nullptr;
            }
        };

        // 计算像素平均值
        int ComputerPixelMean(Mat image, float luminanceMiniThresh, float luminanceMaxThresh) const;

        // 计算清晰度
        int ComputerBlurry(Mat &image, FaceObject face_obj, float blueThresh) const;

        static FaceResult model_predict(cv::Mat &image, float iouNormal, float iouInter, float TLXRatio, float TLYRatio,
            float RBXRatio, float RBYRatio);

    private:
        const Detector &operator=(const Detector &Detector); // 禁止copy 方法
        // const Detector &(Detector &detector); // 禁止copy 属性

        static Detector *g_pDetector; // 唯一的detector 指针
        explicit Detector() {

        }; //构造函数
        float MiniThresh = 100;
        float MaxThresh = 210;
        float BlueThresh = 80; // 图像模糊阈值

    };
}
#endif
